package com.financing.platform.web.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.financing.platform.service.IFpFundCFService;
import com.financing.platform.service.IFpFundCMService;
import com.financing.platform.service.IFpFundCompanyService;
import com.financing.platform.service.IFpFundInfoService;
import com.financing.platform.service.IFpFundManagerService;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;
import com.financing.platform.dto.FpFundCFDTO;
import com.financing.platform.dto.FpFundCMDTO;
import com.financing.platform.dto.FpFundCompanyDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.FpFundManagerDTO;

/**
 * @Description: Controller
 * @author caojiayao
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/fund/")
@Scope("prototype")
public class FpFundCompanyAction  extends BaseAction {
	@Autowired
	private IFpFundCompanyService fpFundCompanyService;
	
	@Autowired
	private IFpFundCMService fpFundCMService;
	
	@Autowired
	private IFpFundCFService fpFundCFService;
	
	@Autowired
	private IFpFundManagerService fpFundManagerService;
	
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	@RequestMapping("/fdCompanyQueryList.shtml")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FpFundCompanyDTO dto,Pager pager) {
		try{
			log.info("Begin query fa user list!");
			
			dto.setBegin(pager.getStartIndex());
			dto.setPageSize(pager.getPageSize());
			
			int count = fpFundCompanyService.countFpFundCompany(dto);
			pager.setTotalCount(count);
			List<FpFundCompanyDTO> list = fpFundCompanyService.listFpFundCompany(dto);
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End querylist!");
		}catch(Exception e){
			log.error("queryList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/fdCompanyAdd.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response,FpFundCompanyDTO dto,String jjbmIds,String jjjlIds) {
		try{
			log.info("Begin add info !");
			
			//参数缺失
			if(StringUtil.isNullOrEmptyStr(dto.getJjgsbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//重复添加
			if(isRepeat(dto.getJjgsbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
		
			DTOdateSet.setCreateInfo(dto, request.getSession());
			
			Integer id =fpFundCompanyService.insertFpFundCompany(dto);
			
			//add 基金公司关联基金经理
			if(jjjlIds != null){
				this.addFpFundCM(jjjlIds, dto.getJjgsbm());
			}
			
			//add 基金公司关联基金信息
			if(jjbmIds != null){
				this.addFpFundCF(jjbmIds, dto.getJjgsbm());
			}
			
			resultMap.getAttribute().put("id",id);
			
			if(id !=null && id >0 ){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End add fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
	//add 基金公司关联基金经理
	private void addFpFundCF(String jjbmIds,String jjgsdm){
		String[] ids = jjbmIds.split(",");
		for (String id : ids) {
			if (!StringUtil.isNullOrEmptyStr(id)) {
				FpFundInfoDTO dto = new FpFundInfoDTO();
				dto.setId(new Integer(id));
				
				// get 基金经理信息
				FpFundInfoDTO fundInfoDTO = fpFundInfoService.getFpFundInfo(dto);
				if (fundInfoDTO != null && fundInfoDTO.getJjbm() != null) {
					//add 基金公司关联基金信息
					FpFundCFDTO fundCFDTO = new FpFundCFDTO();
					fundCFDTO.setJjbm(fundInfoDTO.getJjbm());
					fundCFDTO.setJjgsbm(jjgsdm);
					fundCFDTO.setJjmc(fundInfoDTO.getJjmc());
					
					fpFundCFService.insertFpFundCF(fundCFDTO);
				}
			}
		}
	}
	
	//add 基金公司关联基金经理
	private void addFpFundCM(String jjjlIds,String jjgsdm){
		String[] ids = jjjlIds.split(",");
		for (String id : ids) {
			if (!StringUtil.isNullOrEmptyStr(id)) {
				FpFundManagerDTO dto = new FpFundManagerDTO();
				dto.setId(new Integer(id));
				
				// get 基金经理信息
				FpFundManagerDTO fundManagerDTO = fpFundManagerService.getFpFundManager(dto);
				if (fundManagerDTO != null && fundManagerDTO.getJjjlbm() != null) {
					//add 基金公司关联基金经理
					FpFundCMDTO cmDto = new FpFundCMDTO();
					cmDto.setJjgsbm(jjgsdm);
					cmDto.setJjjlbm(fundManagerDTO.getJjjlbm());
					cmDto.setJlmc(fundManagerDTO.getJjjlmc());
					
					fpFundCMService.insertFpFundCM(cmDto);
				}
			}
		}
	}
	
	@RequestMapping("/fdCompanyUpdate.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,FpFundCompanyDTO dto,String jjjlIds,String jjbmIds) {
		try{
			log.info("Begin update fa user info !");
			
			//参数缺失
			if(StringUtil.isNullOrEmptyStr(dto.getJjgsbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//重复添加
			if(isRepeatUpdate(dto.getJjgsbm(),dto.getId())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			
			int count = fpFundCompanyService.updateFpFundCompany(dto);
			
			//update FpFundCF
			if(jjjlIds != null){
			this.updateFpFundCF(jjbmIds, dto.getJjgsbm());
			}
			
			//update FpFundCF
			if(jjjlIds != null){
			this.updateFpFundCM(jjjlIds, dto.getJjgsbm());
			}
			
			resultMap.getAttribute().put("update count",count);
			if(count >0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End update fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	/**
	 * 判断重复
	 * @Title: isRepeat
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param jjbm
	 * @return Boolean 返回类型
	 * @throws
	 */
	private Boolean isRepeat(String jjgsbm){
		FpFundCompanyDTO dto = new FpFundCompanyDTO();
		dto.setJjgsbm(jjgsbm);
		if(fpFundCompanyService.getFpFundCompany(dto) == null){
			return false;
		}
		return true;
	} 
	
	
	/**
	 * 判断重复
	 * @Title: isRepeat
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param jjbm
	 * @return Boolean 返回类型
	 * @throws
	 */
	private Boolean isRepeatUpdate(String jjbm,Integer id){
		FpFundCompanyDTO dto = new FpFundCompanyDTO();
		dto.setJjgsbm(jjbm);
		dto = fpFundCompanyService.getFpFundCompany(dto);
		if(dto != null && dto.getId().intValue() != id.intValue()){
			return true;
		}
		return false;
	}
	
	@RequestMapping("/fdCompanyDelete.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,
			HttpServletResponse response,FpFundCompanyDTO dto) {
		try{
			log.info("Begin delete fa user info !");
			int count = fpFundCompanyService.delFpFundCompany(dto.getId());
			
			resultMap.getAttribute().put("delete count",count);
			
			if(count > 0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End delete fa user info !");
		}catch(Exception e){
			log.error("delete fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	
	@RequestMapping("/fdCompanyDetail.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request,
			HttpServletResponse response, FpFundCompanyDTO requestDto) {
		try{
			FpFundCompanyDTO dto = fpFundCompanyService.getFpFundCompany(requestDto);
			
			//公司旗下基金
			List<FpFundInfoDTO> fundList = new ArrayList<FpFundInfoDTO>();
			//旗下基金经理
			List<FpFundManagerDTO> managerList = new ArrayList<FpFundManagerDTO>();
			if(dto != null && !StringUtil.isNullOrEmptyStr(dto.getJjgsbm())){
				fundList = fpFundInfoService.listFpFundInfoByCompanyId(dto.getJjgsbm());
				managerList = fpFundManagerService.listFpFundManagerByCompanyId(dto.getJjgsbm());
			}
			
			
			resultMap.setAttribute("detail", dto);
			resultMap.setAttribute("fundList", fundList);
			resultMap.setAttribute("managerList", managerList);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query friendshipLink list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	//add 基金公司关联基金经理
	private void updateFpFundCF(String jjbmIds,String jjgsdm){
		
		//先删除
		FpFundCFDTO dto = new FpFundCFDTO();
		dto.setJjgsbm(jjgsdm);
		List<FpFundCFDTO> fundCFDTOs = fpFundCFService.listFpFundCF(dto);
		for(FpFundCFDTO ddto: fundCFDTOs){
			fpFundCFService.delFpFundCF(ddto.getId());
		}
		
		//后添加
		this.addFpFundCF(jjbmIds, jjgsdm);
		
	}
		
	// add 基金公司关联基金经理
	private void updateFpFundCM(String jjjlIds, String jjgsdm) {
		// 先删除
		FpFundCMDTO dto = new FpFundCMDTO();
		dto.setJjgsbm(jjgsdm);
		List<FpFundCMDTO> fundCMDTOs = fpFundCMService.listFpFundCM(dto);
		for (FpFundCMDTO ddto : fundCMDTOs) {
			fpFundCMService.delFpFundCM(ddto.getId());
		}

		// 后添加
		this.addFpFundCM(jjjlIds, jjgsdm);
	}
}
